#include <EB0/EB0.h>

namespace EB0
{
namespace Entities
{
#pragma region Constructor / Destructor
	// Constructor
	Entity::Entity()
	{
		// Null pointers
		mBodyDef = 0;
		mBody = 0;
	}

	// Destructor
	Entity::~Entity()
	{
		// Null pointers
		if (mBodyDef)
		{
			delete mBodyDef;
			mBodyDef = 0;
		}

		mBody = 0;
	}
#pragma endregion

#pragma region Properties
	// Get Body
	b2Body* Entity::getBody() const
	{
		return mBody;
	}
#pragma endregion

#pragma region Attach / Remove / Frame Start
	// Called whenever the entity is attached to a world
	// Used mostly for creating the body
	void Entity::onAttach(States::World* worldState)
	{
		// Only create the body if there is a definition
		if (mBodyDef)
			mBody = worldState->getWorld()->CreateBody(mBodyDef);
	}

	// Called whenever the entity is removed from a world
	// Used mostly for destroying the body
	void Entity::onRemove(States::World* worldState)
	{
		// Destroy the body
		if (mBody)
		{
			worldState->getWorld()->DestroyBody(mBody);
			mBody = 0;
		}

		// Destroy the body definition
		if (mBodyDef)
		{
			delete mBodyDef;
			mBodyDef = 0;
		}
	}

	// Synchronize the properties of the renderable object with the physical representation
	// To be called prior to rendering the object, after the world has updated
	void Entity::onFrameStart(Window::RenderWindow* window, float dt)
	{
		if (mBody)
		{
			SetPosition(mBody->GetPosition().x * TILE_SIZE, mBody->GetPosition().y * TILE_SIZE);
			SetRotation((float)(mBody->GetAngle() * RAD_TO_DEG)); // need to convert to degrees
		}
	}
#pragma endregion

#pragma region Step - Where most of the AI will go...
	// Step function called for purposes such as AI
	// A pointer to the world state is passed in to access other objects
	void Entity::step(States::World* worldState, float dt)
	{
		// Nothing to do...
	}
#pragma endregion
} // namespace Entities
}